import moment from 'moment';
import { ACTION_TYPES } from 'utils/constant';
const initialState = {
    loading: false,
    msgs: [],
    chatName: '',
    linkmansList: [],
    allMsgs: [],
    linkmans: {}
}


export default function chat(state = initialState, action) {
    switch (action.type) {
        case ACTION_TYPES.MSG_RECEIVE_SUCCESS: 
            return ({
                ...state, 
                msgs: [
                    ...state.msgs,
                    { ...action.data }
                ]            
               
            })
        case ACTION_TYPES.MSG_LIST_SUCCESS:
            return ({
                ...state,
                msgs: [
                    ...state.msgs,
                    // ...action.data
                ],
                chatName: action.chatName
            })
        case ACTION_TYPES.CHAT_RECEIVE_SUCCESS: 
            return ({
                ...state
            })
        case ACTION_TYPES.MSG_RECEIVE_LIST_SUCCESS: 
            return ({
                ...state
            })
        case ACTION_TYPES.MSG_SUCCESS: 
            return ({
                ...state,
                // allMsgs: [...state.allMsgs, ...action.allMsgs]
            })
        case ACTION_TYPES.LINKMANS_MESSAGE:
            const linkmans = Object.assign({}, state.linkmans, action.messages);
            Object.keys(linkmans).forEach(linkman => {
                linkmans[linkman].sort(function(a, b) {
                    return moment(a.create_time).valueOf() - moment(b.create_time).valueOf()
                })
            })
            return ({
                linkmans: Object.assign({}, state.linkmans, linkmans)
            })
        case ACTION_TYPES.UPDATE_LINKMANS_MESSAGE:
            const linkmans1 = { ...state.linkmans };
            linkmans1[action.id] && delete linkmans1[action.id]
            return ({
                linkmans: linkmans1
            })
        case ACTION_TYPES.UPDATE_SELF_MESSAGE: 
            return ({
                linkmans: Object.assign({}, state.linkmans, action.messages)
            })
        case ACTION_TYPES.ADD_LINKMAN_MESSAGE: 
            
            return ({
                linkmans: Object.assign({}, state.linkmans, action.messages)
            })
        default:
            return state;
    }
}